package com.tan.kotlintest.activity

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import com.tan.kotlintest.R
import com.tan.kotlintest.base.BaseActivity
import com.tan.kotlintest.fragment.FocusFragment
import com.tan.kotlintest.fragment.HomeFragment
import com.tan.kotlintest.fragment.MessageFragment
import com.tan.kotlintest.fragment.MineFragment
import kotlinx.android.synthetic.main.activity_home.*

/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述：测试Kotlin项目主页
 * 作者：txj
 * 创建时间：2018/9/14
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class HomeActivity : BaseActivity(), View.OnClickListener {

    private val HOME = 0//首页
    private val FOCUS = 1//关注
    private val MESSAGE = 2//消息
    private val MINE = 3//我的

    override fun getContentView(): View? {
        return inflate(R.layout.activity_home)
    }

    override fun onContentAdded() {
        needHeader(false)
        initView()
        initViewPager()
        switchTab(HOME, false)
    }

    private fun initView() {
        home_ll.setOnClickListener(this)
        focus_ll.setOnClickListener(this)
        message_ll.setOnClickListener(this)
        mine_ll.setOnClickListener(this)
    }

    /**
     * 初始化ViewPager
     */
    private fun initViewPager() {
        val list = ArrayList<Fragment>()
        val homeFragment = HomeFragment()
        val focusFragment = FocusFragment()
        val messageFragment = MessageFragment()
        val mineFragment = MineFragment()
        list.add(0, homeFragment)
        list.add(1, focusFragment)
        list.add(2, messageFragment)
        list.add(3, mineFragment)
        content_vp.adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return list[position]
            }

            override fun getCount(): Int {
                return list.size
            }

        }
        content_vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                switchTab(position, true)
            }
        })
        content_vp.offscreenPageLimit = 4
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.home_ll -> {//首页
                switchTab(HOME, false)
            }
            R.id.focus_ll -> {//关注
                switchTab(FOCUS, false)
            }
            R.id.message_ll -> {//消息
                switchTab(MESSAGE, false)
            }
            R.id.mine_ll -> {//我的
                switchTab(MINE, false)
            }
        }
    }

    private fun switchTab(position: Int, fromViewPager: Boolean) {
        when (position) {
            HOME -> {
                if (home_iv.isSelected || home_tv.isSelected) {
                    return
                }
                if (!fromViewPager) {
                    content_vp.currentItem = HOME
                }
                home_iv.isSelected = true
                home_tv.isSelected = true
                focus_iv.isSelected = false
                focus_tv.isSelected = false
                message_iv.isSelected = false
                message_tv.isSelected = false
                mine_iv.isSelected = false
                mine_tv.isSelected = false
            }
            FOCUS -> {
                if (focus_iv.isSelected || focus_tv.isSelected) {
                    return
                }
                if (!fromViewPager) {
                    content_vp.currentItem = FOCUS
                }
                focus_iv.isSelected = true
                focus_tv.isSelected = true
                home_iv.isSelected = false
                home_tv.isSelected = false
                message_iv.isSelected = false
                message_tv.isSelected = false
                mine_iv.isSelected = false
                mine_tv.isSelected = false
            }
            MESSAGE -> {
                if (message_iv.isSelected || message_tv.isSelected) {
                    return
                }
                if (!fromViewPager) {
                    content_vp.currentItem = MESSAGE
                }
                message_iv.isSelected = true
                message_tv.isSelected = true
                home_iv.isSelected = false
                home_tv.isSelected = false
                focus_iv.isSelected = false
                focus_tv.isSelected = false
                mine_iv.isSelected = false
                mine_tv.isSelected = false
            }
            MINE -> {
                if (mine_iv.isSelected || mine_tv.isSelected) {
                    return
                }
                if (!fromViewPager) {
                    content_vp.currentItem = MINE
                }
                mine_iv.isSelected = true
                mine_tv.isSelected = true
                home_iv.isSelected = false
                home_tv.isSelected = false
                focus_iv.isSelected = false
                focus_tv.isSelected = false
                message_iv.isSelected = false
                message_tv.isSelected = false
            }
        }
    }

}